package com.wang.lottery.application.mp.producer;

import cn.hutool.json.JSONUtil;
import com.wang.lottery.domain.activity.model.vo.ActivityPartakeRecordVO;
import com.wang.lottery.domain.activity.model.vo.InvoiceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import javax.annotation.Resource;

/**
 * @author wjh
 * @create 2023-11-12 17:11
 * 消息生产者
 * MQ主题：中奖发货单
 * 启动zk：bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
 * 启动kafka：bin/kafka-server-start.sh -daemon config/server.properties
 * 创建topic：bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic lottery_invoice
 */
@Component
@Slf4j
public class KafkaProducer {
    
    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;
    
    public static final String TOPIC_LOTTERY_RESULT = "lottery_invoice";
    public static final String TOPIC_LOTTERY_GROUP = "lottery";
    public static final String TOPIC_ACTIVITY_PARTAKE = "lottery_activity_partake";
    public ListenableFuture<SendResult<String, Object>> sendLotteryInvoice(InvoiceVO invoiceVO){
        String jsonStr = JSONUtil.toJsonStr(invoiceVO);
        return kafkaTemplate.send(TOPIC_LOTTERY_RESULT, jsonStr);
        
    }

    public ListenableFuture<SendResult<String, Object>> sendLotteryActivityPartakeRecord(ActivityPartakeRecordVO activityPartakeRecordVO) {
        String jsonStr = JSONUtil.toJsonStr(activityPartakeRecordVO);
        return kafkaTemplate.send(TOPIC_ACTIVITY_PARTAKE,jsonStr);
    }
}
